package cn.xenosp.server.module.activiti.service.impl;

import cn.xenosp.server.common.error.ActivitiBusinessException;
import cn.xenosp.server.common.tools.ConverterUtil;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.activiti.entity.ProcessInstanceVO;
import cn.xenosp.server.module.activiti.mapper.ActRuExecutionMapper;
import cn.xenosp.server.module.activiti.service.ProcessInstanceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.module.activiti.service.impl
 * 作者：   TongHui
 * 创建时间: 2025-06-06 15:07
 * 描述: TODO
 * 版本: 1.0
 */
@Slf4j
@Service
public class ProcessInstanceServiceImpl extends ServiceImpl<ActRuExecutionMapper, ProcessInstanceVO>
        implements ProcessInstanceService {

    //运行时的接口
    @Autowired
    private RuntimeService runtimeService;
    // 任务处理接口
    @Autowired
    private TaskService taskService;


    @Override
    public IPage getByPaged(ProcessInstanceVO processInstance) {
        Page page = new Page(processInstance.getPage(), processInstance.getLimit());
        IPage ipage = baseMapper.page(page,processInstance);

/*        ProcessInstanceQuery condition = runtimeService.createProcessInstanceQuery();
        List<ProcessInstance> processList = condition.orderByProcessDefinitionId().desc().listPage(processInstance.getPage(), processInstance.getLimit());
        List<ProcessInstanceVO> flows = new ArrayList<>();
        processList.stream().forEach(p -> {
           ProcessInstanceVO info = new ProcessInstanceVO();
            ObjectConvert.copyVoToEntity(p,info);
             *//*info.setProcessInstanceId(p.getProcessInstanceId());
            info.setBusinessKey(p.getBusinessKey());
            info.setName(p.getProcessDefinitionName());
            info.setStartTime(p.getStartTime());
            info.setStartUserId(p.getStartUserId());
            info.setSuspended(p.isSuspended());
            info.setSuspendedName(!p.isSuspended()?"已激活":"已挂起");
            info.setEnded(p.isEnded());*//*
            // 查看当前活动任务
            if (info.getTaskCount() > 0){
                List<Task> tasks =  taskService.createTaskQuery().processInstanceId(p.getProcessInstanceId()).list();
                if (tasks.size() > 0) {
                    String taskName = "";
                    String assignee = "";
                    for (Task t : tasks) {
                        taskName += t.getName() + ",";
                        assignee += t.getAssignee() + ",";
                    }
                    taskName = taskName.substring(0, taskName.length() -1);
                    assignee = assignee.substring(0, assignee.length() -1);
                    info.setCurrentTask(taskName);
                    info.setAssignee(assignee);
                }
            }
            flows.add(info);
        });
        int total = condition.orderByProcessDefinitionId().desc().list().size();

        page.setRecords(flows);
        page.setTotal(total);*/
        return ipage;
    }



    @Override
    public void completeTask(String id) {
        //根据流程实例id，查询任务
        List<Task> taskList = taskService.createTaskQuery().processInstanceId(id).list();
        if (taskList.size() != 1) {
            throw new ActivitiBusinessException("当前没有任务");
        }
        log.info("任务列表：{}", taskList);
        //根据任务id，完成任务
        taskService.complete(taskList.get(0).getId());
    }
}
